package com.oppo.MarkBook.window;

import com.intellij.notification.Notification;
import com.intellij.notification.NotificationDisplayType;
import com.intellij.notification.NotificationGroup;
import com.intellij.notification.Notifications;
import com.intellij.openapi.fileChooser.FileChooser;
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.MessageDialogBuilder;
import com.intellij.openapi.ui.MessageType;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.ToolWindow;
import com.oppo.MarkBook.data.DataCenter;
import com.oppo.MarkBook.processor.DefaultSourceNoteData;
import com.oppo.MarkBook.processor.MDFreeMarkProcessor;
import com.oppo.MarkBook.processor.Processor;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * @author steve
 * @since 2021-12-18 23:50
 */

public class NoteListWindow {
    private JTextField tfTopic;
    private JTable tbContent;
    private JButton btnCreate;
    private JButton btnClose;
    private JButton btnClear;
    private JPanel contentPanel;

    /**
     * 定义表格初始化设置，并在`NoteListWindow`构造方法中调用`init`
     */
    private void init() {
        tbContent.setModel(DataCenter.TABLE_MODEL);
        tbContent.setEnabled(true);
    }

    public NoteListWindow(Project project, ToolWindow toolWindow) {
        init();
        btnCreate.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String topic = tfTopic.getText();
                // 生成文档的文件名和文件类型
                String fileName = topic + ".md";
                if (topic == null || "".equals(topic)) {
                    MessageDialogBuilder.yesNo("操作结果", "文档标题没有输入");
                    // 弹窗通知
                    notifyMsg("md_plugin", "请输入文档标题");
                    return;
                }
                // 弹窗中用户选择的目录
                VirtualFile virtualFile = FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(),
                        project, project.getBaseDir());
                if (virtualFile != null) {
                    String path = virtualFile.getPath();
                    String fileFullPath = path + File.separator + fileName;

                    Processor processor = new MDFreeMarkProcessor();
                    try {
                        processor.processor(new DefaultSourceNoteData(fileFullPath, topic, DataCenter.NOTE_LIST));
                        notifyMsg("md_plugin_id", "生成文档完成");
                    } catch (Exception exp) {
                        exp.printStackTrace();
                    }
                }
            }
        });

        btnClose.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                toolWindow.hide();
            }
        });

        btnClear.addActionListener(e -> DataCenter.reset());
    }

    /**
     * 消息通知-event log 日志消息
     *
     * @param displayId 显示id
     * @param content   消息内容
     */
    private void notifyMsg(String displayId, String content) {
        NotificationGroup notificationGroup = new NotificationGroup(displayId, NotificationDisplayType.BALLOON, true);
        /*
          content :  通知内容
          type  ：通知的类型，warning,info,error
         */
        Notification notification = notificationGroup.createNotification(content, MessageType.INFO);
        Notifications.Bus.notify(notification);
    }

    public JPanel getContentPanel() {
        return contentPanel;
    }
}
